<?php
// +----------------------------------------------------------------------
// | Demo [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lmx0536.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: limx <715557344@qq.com> <http://www.lmx0536.cn>
// +----------------------------------------------------------------------
// | Date: 2016/12/4 Time: 17:29
// +----------------------------------------------------------------------
namespace MyApp\Tasks;

use MyApp\Models\Admin;
use Phalcon\Cli\Task;

class InitTask extends Task
{
    public function mainAction($params = [])
    {
        $username = "";
        $password = "";
        foreach ($params as $v) {
            $item = explode('=', $v);
            switch ($item[0]) {
                case '-u':
                    $username = $item[1];
                    break;
                case '-p':
                    $password = $item[1];
                    break;
            }
        }
        if (empty($username) || empty($password)) {
            echo "Please check your input. \n";
            echo "You can input php run Init help to see what to do. \n";
        }
        $admin = Admin::findFirst([
            "conditions" => "username=?0",
            "bind" => [$username]
        ]);
        if ($admin) {
            $admin->password = password($password);
        } else {
            $admin = new Admin();
            $admin->username = $username;
            $admin->password = password($password);
        }
        if ($admin->save()) {
            echo "Sucess!\n";
        } else {
            echo "Error!\n";
        }
    }

    public function helpAction()
    {
        echo "php run Init main -u=username -p=password\n";
    }


}